package com.coolw.test.compare.diff;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * 模板数据字段值比较差异工具类
 *
 * @author coolw
 * @date 2024/10/9 19:29
 */
public final class TemplateDataDiffUtil {

    /**
     * 创建字段差异信息对象
     *
     * @return 字段差异信息对象
     */
    private static FieldDiffInfo createFieldDiffInfo() {
        return new FieldDiffInfo();
    }

    /**
     * 借款模板数据比较
     *
     * @param oldTemplateData     历史数据
     * @param currentTemplateData 当前数据
     * @return 比较结果
     */
    public static FieldDiffInfo compareCashTemplateData(CashTemplateData oldTemplateData, CashTemplateData currentTemplateData) {
        FieldDiffInfo fieldDiffInfo = createFieldDiffInfo();
        compareInteger(fieldDiffInfo, "loanTerm", oldTemplateData.getLoanTerm(), currentTemplateData.getLoanTerm());
        compareInteger(fieldDiffInfo, "repayDay", oldTemplateData.getRepayDay(), currentTemplateData.getRepayDay());
        compareString(fieldDiffInfo, "loanCashDate", oldTemplateData.getLoanCashDate(), currentTemplateData.getLoanCashDate());
        compareBigDecimal(fieldDiffInfo, "perMonthRepayAmt", oldTemplateData.getPerMonthRepayAmt(), currentTemplateData.getPerMonthRepayAmt());
        compareBankCardInfo(fieldDiffInfo, "bankCardInfo", oldTemplateData.getBankCardInfo(), currentTemplateData.getBankCardInfo());
        return fieldDiffInfo;
    }

    /**
     * String类型值比较
     *
     * @param fieldDiffInfo 字段差异信息
     * @param fieldName     字段名称
     * @param value1        值1
     * @param value2        值2
     */
    public static void compareString(FieldDiffInfo fieldDiffInfo, String fieldName, String value1, String value2) {
        if (!Objects.equals(value1, value2)) {
            fieldDiffInfo.addDiff(fieldName, value1, value2);
        }
    }

    /**
     * Integer类型值比较
     *
     * @param fieldDiffInfo 字段差异信息
     * @param fieldName     字段名称
     * @param value1        值1
     * @param value2        值2
     */
    public static void compareInteger(FieldDiffInfo fieldDiffInfo, String fieldName, Integer value1, Integer value2) {
        if (!Objects.equals(value1, value2)) {
            fieldDiffInfo.addDiff(fieldName
                    , value1 != null ? value1.toString() : null
                    , value2 != null ? value2.toString() : null);
        }
    }

    /**
     * BigDecimal类型值比较
     *
     * @param fieldDiffInfo 字段差异信息
     * @param fieldName     字段名称
     * @param value1        值1
     * @param value2        值2
     */
    public static void compareBigDecimal(FieldDiffInfo fieldDiffInfo, String fieldName, BigDecimal value1, BigDecimal value2) {
        if (!Objects.equals(value1, value2)) {
            fieldDiffInfo.addDiff(fieldName
                    , value1 != null ? value1.toString() : null
                    , value2 != null ? value2.toString() : null);
        }
    }

    /**
     * 银行卡信息数据比较
     *
     * @param fieldDiffInfo 字段差异信息
     * @param fieldName     字段名称
     * @param info1         数据1
     * @param info2         数据2
     */
    public static void compareBankCardInfo(FieldDiffInfo fieldDiffInfo, String fieldName, BankCardInfo info1, BankCardInfo info2) {
        if (info1 == null && info2 == null) {
            return;
        }
        if (info1 == null || info2 == null) {
            fieldDiffInfo.addDiff(fieldName
                    , info1 != null ? info1.toString() : "null"
                    , info2 != null ? info2.toString() : "null");
            return;
        }
        compareString(fieldDiffInfo, fieldName + ".cardNo", info1.getCardNo(), info2.getCardNo());
    }
}
